package component.fulltextsearch.entity;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;


/**
 *	通用UserDocument对象：
 *	当有多个文档都要索引,而这些文档刚好又有很多的属性是相同的。
 *	则可以使用该类来创建文档对象。这样可以提高建立索引的效率。
 *	构造方法为
 *	CommonUserDocument(UserDocument common, int commonFields)
 *	第一个参数是一个UserDocument,该doc中包含了公共的属性的值
 *	第二个参数指名那些事公共属性的值,改值表示方法类似SWT用法,
 *	   例如:如果content、cummary 、attachmentPaths三个属性为
 *	公共属性，则该值为:
 *	COMMON_CONTENT|COMMON_SUMMARY|COMMON_ATTACHMENTPATHS
 *	
 *	其他私有属性的部分，通过创建不同的UserDocument对象,
 *	调用addPrivateDocument方法添加到CommonUserDocument中。
 *
 */
public class CommonUserDocument {
	
	private UserDocument common;

	public static final int COMMON_NONE = 0;
	public static final int COMMON_KEYVALUE = 1 << 1;	
	public static final int COMMON_RIGHTUSERS = 1 << 2;
	public static final int COMMON_RIGHTROLES = 1 << 3;
	public static final int COMMON_TYPECODE = 1 << 4;
	public static final int COMMON_NAME = 1 << 5;
	public static final int COMMON_KEYWORDS = 1 << 6;
	public static final int COMMON_SUMMARY = 1 << 7;
	public static final int COMMON_CONTENT = 1 << 8;
	public static final int COMMON_ATTACHMENTPATHS = 1 << 9;
	public static final int COMMON_ATTACHMENTCONTENT = 1 << 10;	
	public static final int COMMON_CREATETIME = 1 << 11;
	public static final int COMMON_BUSINESSSTRING = 1 << 12;
	
	
	private int commonFields = COMMON_NONE;
	
	private Set privateDocument = new HashSet();
	
	public Set getPrivateDocument() {
		return privateDocument;
	}

	/**
	 * 通用UserDocument对象。
	 *	当有多个文档都要索引,而这些文档刚好又有很多的属性是相同的。
	 *	则可以使用该类来创建文档对象。这样可以提高建立索引的效率。
	 *	构造方法为
	 *	CommonUserDocument(UserDocument common, int commonFields)
	 *	第一个参数是一个UserDocument,该doc中包含了公共的属性的值
	 *	第二个参数指名那些事公共属性的值,改值表示方法类似SWT用法,
	 *	   例如:如果content、cummary 、attachmentPaths三个属性为
	 *	公共属性，则该值为:
	 *	COMMON_CONTENT|COMMON_SUMMARY|COMMON_ATTACHMENTPATHS
	 *	
	 *	其他私有属性的部分，通过创建不同的UserDocument对象,
	 *	调用addPrivateDocument方法添加到CommonUserDocument中。
	 * @param common
	 * @param commonFields
	 */
	public CommonUserDocument(UserDocument common, int commonFields) {
		this.common = common;
		this.commonFields = commonFields;
	}
	
	/**
	 * 添加一个私有的UserDocument
	 * @param privateDocument
	 */
	public void addPrivateDocument(UserDocument privateDocument) {
		this.privateDocument.add(privateDocument);
	}
	
	public Set getFinalUserDocuments() {
		Set set = new HashSet();
		for (Iterator it = set.iterator(); it.hasNext();) {
			UserDocument pUserDoc = (UserDocument) it.next();
			UserDocument userDoc = new UserDocument();
			if ((commonFields & COMMON_KEYVALUE) > 0 ) { 
				userDoc.setKeyValue(common.getKeyValue());
			} else {
				userDoc.setKeyValue(common.getKeyValue());
			}
			if ((commonFields & COMMON_NAME) > 0 ) { 
				userDoc.setName(common.getName());
			} else {
				userDoc.setName(common.getName());
			}
			if ((commonFields & COMMON_KEYWORDS) > 0 ) { 
				userDoc.setKeywords(common.getKeywords());
			} else {
				userDoc.setKeywords(common.getKeywords());
			}
			if ((commonFields & COMMON_SUMMARY) > 0 ) { 
				userDoc.setSummary(common.getSummary());
			} else {
				userDoc.setSummary(common.getSummary());
			}
			if ((commonFields & COMMON_CONTENT) > 0 ) { 
				userDoc.setContent(common.getContent());
			} else {
				userDoc.setContent(common.getContent());
			}				
			if ((commonFields & COMMON_ATTACHMENTPATHS) > 0 ) { 
				userDoc.setAttachmentPaths(common.getAttachmentPaths());
			} else {
				userDoc.setAttachmentPaths(common.getAttachmentPaths());
			}
			if ((commonFields & COMMON_ATTACHMENTCONTENT) > 0 ) { 
				userDoc.setAttachmentContent(common.getAttachmentContent());
			} else {
				userDoc.setAttachmentContent(common.getAttachmentContent());
			}
			if ((commonFields & COMMON_RIGHTUSERS) > 0 ) { 
				userDoc.setRightUsers(common.getRightUsers());
			} else {
				userDoc.setRightUsers(common.getRightUsers());
			}
			if ((commonFields & COMMON_RIGHTROLES) > 0 ) { 
				userDoc.setRightRoles(common.getRightRoles());
			} else {
				userDoc.setRightRoles(common.getRightRoles());
			}				
			if ((commonFields & COMMON_CREATETIME) > 0 ) { 
				userDoc.setCreateTime(common.getCreateTime());
			} else {
				userDoc.setCreateTime(common.getCreateTime());
			}
			if ((commonFields & COMMON_BUSINESSSTRING) > 0 ) { 
				userDoc.setBusinessString(common.getBusinessString());
			} else {
				userDoc.setBusinessString(common.getBusinessString());
			}
			if ((commonFields & COMMON_TYPECODE) > 0 ) { 
				userDoc.setTypeCode(common.getTypeCode());
			} else {
				userDoc.setTypeCode(common.getTypeCode());
			}
			set.add(userDoc);
		}		
		return set;
		
	}
	
	public int getCommonFields() {
		return this.commonFields;
	}
	
	public static void main(String[] args) {	
		UserDocument doc = new UserDocument(); 
		
		UserDocument doc1 = new UserDocument(); 
		UserDocument doc2 = new UserDocument(); 
		UserDocument doc3 = new UserDocument(); 
		CommonUserDocument cdoc = new CommonUserDocument(doc,CommonUserDocument.COMMON_NAME|CommonUserDocument.COMMON_KEYWORDS
				|CommonUserDocument.COMMON_SUMMARY|CommonUserDocument.COMMON_CONTENT|CommonUserDocument.COMMON_ATTACHMENTCONTENT);
		cdoc.addPrivateDocument(doc1);
		cdoc.addPrivateDocument(doc2);
		cdoc.addPrivateDocument(doc3);
		
		int x = COMMON_ATTACHMENTCONTENT | COMMON_KEYWORDS|COMMON_BUSINESSSTRING ;
		
		System.out.println(x & COMMON_KEYVALUE);
		System.out.println(x & COMMON_NAME);
		System.out.println(x & COMMON_KEYWORDS);
		System.out.println(x & COMMON_SUMMARY);
		System.out.println(x & COMMON_CONTENT);
		
		System.out.println(x & COMMON_ATTACHMENTPATHS);
		System.out.println(x & COMMON_ATTACHMENTCONTENT);
		System.out.println(x & COMMON_RIGHTUSERS);
		System.out.println(x & COMMON_RIGHTROLES);
		
		System.out.println(x & COMMON_CREATETIME);
		System.out.println(x & COMMON_BUSINESSSTRING);
		System.out.println(x & COMMON_TYPECODE);
	}
	
	
}

